/*
 * ===================================================
 *
 *       Filename:  parse_trace.c
 *
 *
 *        Created:  10/20/2011 03:44:19 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  durand_y, <durand.yoan@gmail.com>
 *
 * ===================================================
 */
#include "parser.h"
#include <stdio.h>

static int      check_trace(char    *trace,
                            char    *sub,
                            int     len)
{
  int           err = 0;
  int           i = 0;

  for (int j = 0; j < len ; j++)
  {
    sub[i] = trace[j];
    i++;

    if (trace[j] == '.')
    {
      i = 0;
      if (((mystrcmp(sub, ".") == 0))
          && (mystrcmp(sub, "a.") == 0)
          && (mystrcmp(sub, "b.") == 0)
          && (mystrcmp(sub, "l.") == 0)
          && (mystrcmp(sub, "r.") == 0)
          && (mystrcmp(sub, "ar.") == 0)
          && (mystrcmp(sub, "al.") == 0))
      {
        err = 1;
        error(1, (j - mystrlen(sub) + 1), 0);
        return (err);
      }
    }
  }

  return (err);
}

int             parse_trace(char *trace)
{
  int           err = 0;
  char          *sub = 0;
  int           len = 1;
  s_car         *car = NULL;

  len = mystrlen(trace);
  sub = malloc(sizeof (char) * 4);
  car = malloc(sizeof (s_car));
  err = check_trace(trace, sub, len);
  return (err);
}

static void     run_cmd2(char   *sub,
                         s_car  **car)
{
  if (mystrcmp(sub, "r.") == 1)
  {
    turning_right(car);
    deceleration(car);
    movement(car);
    (*car)->cycle++;
  }
  if (mystrcmp(sub, "ar.") == 1)
  {
    acceleration(car);
    turning_right(car);
    deceleration(car);
    movement(car);
    (*car)->cycle++;
  }
  if (mystrcmp(sub, "al."))
  {
    acceleration(car);
    turning_right(car);
    deceleration(car);
    movement(car);
    (*car)->cycle++;
  }
}

static void     run_cmd(char    *sub,
                        s_car   **car)
{
  if (mystrcmp(sub, "a.") == 1)
  {
    acceleration(car);
    deceleration(car);
    movement(car);
    (*car)->cycle++;
  }
  if (mystrcmp(sub, "b.") == 1)
  {
    braking(car);
    deceleration(car);
    movement(car);
    (*car)->cycle++;
  }
  if (mystrcmp(sub, "l.") == 1)
  {
    turning_left(car);
    deceleration(car);
    movement(car);
    (*car)->cycle++;
  }
  run_cmd2(sub, car);
}

s_car           *run_trace(char    *trace,
                           s_car   *car,
                           char    **map)
{
  int           j = 0;
  char          *sub = NULL;

  sub = malloc(sizeof(char) * 4);

  for (int i = 0; i < mystrlen(trace); i++)
  {
    sub[j] = trace[i];
    j++;
    if (trace[i] == '.')
    {
      j = 0;
      run_cmd(sub, &car);
     if (map[car->x >> 16][car->y >> 16] != '.')
     {
        if ((map[car->x >> 16][car->y >> 16] != '!') &&
            (map[car->x >> 16][car->y >> 16] != '*'))
        {
          error(2, (car->x >> 16), (car->y >> 16));
          return (NULL);
        }
        if (map[car->x >> 16][car->y >> 16] == '!')
        {
          finish(car);
          return (car);
        }
      }
    }
  }
  return (car);
}
